package com.feeyo.groundservice.base.rabbitmq;

import com.feeyo.groundservice.base.util.PropertiesUtils;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.json.JsonObject;
import io.vertx.rabbitmq.RabbitMQClient;

/**
 * Created by fooevr on 2016/12/1.
 */
public class RMQ extends AbstractVerticle{
    private RabbitMQClient client;
    private static RMQ instance = new RMQ();

    @Override
    public void start(Future<Void> startFuture) throws Exception {
        JsonObject config = new JsonObject();
        config.put("user", PropertiesUtils.getProperty("rabbitmq.user"));
        config.put("password", PropertiesUtils.getProperty("rabbitmq.password"));
        config.put("host", PropertiesUtils.getProperty("rabbitmq.host"));
        config.put("port", Integer.parseInt(PropertiesUtils.getProperty("rabbitmq.port")));
        config.put("connectionTimeout", Integer.parseInt(PropertiesUtils.getProperty("rabbitmq.connectionTimeout")));
        config.put("automaticRecoveryEnabled", true);
        config.put("networkRecoveryInterval", Integer.parseInt(PropertiesUtils.getProperty("rabbitmq.networkRecoveryInterval")));
        client = RabbitMQClient.create(vertx, config);
        client.start(ar->{
            if (ar.failed()){
                System.out.println("start connect to rmq faield " + ar.cause().getMessage());
            }
            if(ar.succeeded()){
                startFuture.complete();
            }
        });
        instance = this;
    }

    public static RMQ getRMQInstance(){
        return instance;
    }

    public RabbitMQClient getRMQClient(){
        return client;
    }
}
